﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Collections.Specialized;

namespace MyVoteMVC.Common
{
    /***************************
     * IConfigurationSectionHandler支持的格式太少了
     * 选择ConfigurationSection进行更多的自定义配置
     * ****************************/
    public class FactoryConfigHandler : IConfigurationSectionHandler
    {
        public object Create(object parent, object configContext, System.Xml.XmlNode section)
        {
            NameValueCollection configs;
            NameValueSectionHandler baseHandler = new NameValueSectionHandler();
            configs = (NameValueCollection)baseHandler.Create(parent, configContext, section);
            return configs;
        }
    }

    public class FactoryConfigSection : ConfigurationSection
    {
        [ConfigurationProperty("Types", IsDefaultCollection = true)]
        public TypeConfigurationElementCollection Types
        {
            get
            {
                return (TypeConfigurationElementCollection)base["Types"];
            }
        }
    }

    public class TypeConfigurationElementCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new TypeConfigurationElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((TypeConfigurationElement)element).name;
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }
        protected override string ElementName
        {
            get
            {
                return "Type";
            }
        }

        public TypeConfigurationElement this[int index]
        {
            get
            {
                return (TypeConfigurationElement)BaseGet(index);
            }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }
    }

    public class TypeConfigurationElement : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true)]
         public string name
         {
             get
             {
                 return (string)base["name"];
             }
             set
             {
                 base["name"] = value;
             }
         }
        [ConfigurationProperty("mapto", IsRequired = true)]
        public string mapto
        {
            get
            {
                return (string)base["mapto"];
            }
            set
            {
                base["mapto"] = value;
            }
        }
    
    }


        
}
